var currentPage = 0
/*
currentSearch == -1 权限全部搜索
currentSearch == -2 权限条件搜索/
 */
var currentSearch = -1
$(document).ready(function () {
    addTimeOutAction()
    $('#select-all').on('click', function () {
        if(this.checked == true){
            $('.select-sub-box').prop('checked',true)
        }
        else{
            $('.select-sub-box').prop('checked',false)
        }
    })

    getAllContractStatusInformation()

    /*
搜索添加回车绑定事件/
*/
    $('#search-ContractStatusName').on('keypress', function (event) {
        if(event.keyCode == '13'){
            searchContractStatusByName()
        }
    })
})
/*
/添加合同状态
 */
function addContractStatus() {
    var ContractStatusName = $('#modal-ContractStatusName').val()
    if(!ContractStatusName){
        alert("请输入合同状态名称！")
        return
    }else{
        var urlStr = ipPort + '/contractStatus/add?name=' + ContractStatusName
        $.ajax({
            url:urlStr,
            dataType:'json',
            success:function (obj) {
                if(obj.code == 8){
                    alert(obj.message)
                }else{
                    alert(obj.message)
                    getAllContractStatusInformation()
                }
            },
            error:function (error) {
                console.log(error)
            }
        })
    }
}
/*
设置修改面板信息/
 */
function setModifyModalInformation(obj) {
    var td = $(obj).parent().parent().find('td')
    $('#modal-modifyContractStatusID').val(td.eq(0).text())
    $('#modal-modifyContractStatusName').val(td.eq(1).text())
}
/*
修改合同状态信息/
 */
function modifyContractStatus() {
    var ContractStatusID = $('#modal-modifyContractStatusID').val()
    var ContractStatusName = $('#modal-modifyContractStatusName').val()
    if(!ContractStatusName){
        alert("名称不可为空！")
        return
    }
    var urlStr = ipPort + '/contractStatus/update?id='+ ContractStatusID + "&name=" + ContractStatusName
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 8){
                alert(obj.message)
            }
            else if(obj.code == 0){
                alert("修改合同状态信息成功！")
                getAllContractStatusInformation()
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
删除合同状态信息/
 */
function deleteContractStatus(thisObj) {
    var td = $(thisObj).parent().parent().find('td')
    var ContractStatusID = td.eq(0).text()
    var urlStr = ipPort + '/contractStatus/deleteById?id='+ ContractStatusID
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 0){
                alert("删除合同状态信息成功！")
                getAllContractStatusInformation()
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
批量删除合同状态信息/
 */
function deleteContractStatusInBatch() {
    var select_sub_box = $('.select-sub-box')
    var jsonArr = []
    for(var i = 0; i < select_sub_box.length; i++){
        if(select_sub_box.eq(i).is(':checked') == true){
            var json = {}
            json['id'] = parseInt(select_sub_box.eq(i).attr('value'));
            jsonArr.push(json)
        }
    }
    let myjson = JSON.stringify(jsonArr)
    var urlStr = ipPort + '/contractStatus/deleteByIdBatch'
    $.ajax({
        url:urlStr,
        contentType:'application/json',
        data:myjson,
        dataType:'json',
        type:'post',
        success:function (obj) {
            if(obj.code == 0){
                alert("批量删除合同状态信息成功！")
                getAllContractStatusInformation()
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
通过名称搜索合同状态信息/
 */
function searchContractStatusByName(page_ = 0) {
    var ContractStatusName = $('#search-ContractStatusName').val()
    if(!ContractStatusName){
        alert('请输入合同状态名称')
        return
    }
    currentSearch = -2
    currentPage = page_
    var size = 10
    var sortFieldName = 'id'
    var asc = 1
    var urlStr = ipPort + '/contractStatus/getByNameLikeByPage?page='+ currentPage + '&size=' + size + '&sortFieldName=' + sortFieldName + '&asc=' + asc + '&name=' + ContractStatusName
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 0){
                setContractStatusTableInformation(obj)
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
获取全部合同状态信息/
 */
function getAllContractStatusInformation(page_ = 0) {
    currentSearch = -1
    currentPage = page_
    var size = 10
    var sortFieldName = 'id'
    var asc = 1
    var urlStr = ipPort + '/contractStatus/getAllByPage?page='+ currentPage + '&size=' + size + '&sortFieldName=' + sortFieldName + '&asc=' + asc
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            setContractStatusTableInformation(obj)
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
设置合同状态table信息/
 */
function setContractStatusTableInformation(obj) {
    $('#allContractStatusInformationPanel .currentPage').text(currentPage + 1)
    $('#allContractStatusInformationPanel .totalPage').text(obj.data.totalPages)

    var table_tr = $('.table-tr')
    var ContractStatus_id = $('.contractStatus-id')
    var ContractStatus_name = $('.contractStatus-name')
    for(var i = 0; i < obj.data.numberOfElements; i++){
        table_tr.eq(i).removeClass('hidden')
        ContractStatus_id.eq(i).html("<input class=\"select-box select-sub-box\" type=\"checkbox\"" +  "value=\"" + obj.data.content[i].id + "\"" + ">" + obj.data.content[i].id)
        ContractStatus_name.eq(i).text(obj.data.content[i].name)
    }
    for (var i = obj.data.numberOfElements; i < 10; i++){
        table_tr.eq(i).addClass('hidden')
    }
}


/*
上一页/
 */
function previousPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    if(currentPage_ == 1){
        alert("已经是第一页！")
        return
    }
    currentPage--
    if(currentPage < 0){
        currentPage = 0
    }
    if(currentSearch == -1){
        getAllContractStatusInformation(currentPage)
    }else  if(currentSearch == -2){
        searchContractStatusByName(currentPage)
    }

}
/*
下一页/
 */
function nextPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    var totalPage_ = $(str).find('.totalPage').text()
    if(currentPage_ == totalPage_){
        alert("已经是最后一页！")
        return
    }
    currentPage++
    if(currentSearch == -1){
        getAllContractStatusInformation(currentPage)
    }else  if(currentSearch == -2){
        searchContractStatusByName(currentPage)
    }




}
/*
跳转页/
 */
function skipPage(str) {
    var skipPage_ = parseInt($(str).find('.skipPage').val())
    var totalPage_ = parseInt($(str).find('.totalPage').text())
    if (skipPage_ - totalPage_ > 0) {
        alert("没有此页！")
        return
    }
    if (skipPage_ < 1) {
        alert("没有此页！")
        return
    }
    currentPage = skipPage_ - 1
    if (currentSearch == -1) {
        getAllContractStatusInformation(currentPage)
    } else if (currentSearch == -2) {
        searchContractStatusByName(currentPage)
    }
}